package www.dianbaozu.com.ui.chat.friend

import android.util.Log
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.netease.nim.uikit.api.NIMHelper
import com.netease.nim.uikit.api.NimUIKit
import com.netease.nim.uikit.api.SessionHelper
import com.netease.nim.uikit.api.model.contact.ContactChangedObserver
import com.netease.nim.uikit.api.model.user.UserInfoObserver
import com.netease.nim.uikit.common.ui.recyclerview.listener.SimpleClickListener
import com.netease.nimlib.sdk.msg.constant.SystemMessageType
import www.dianbaozu.com.R
import www.dianbaozu.com.databinding.FragmentFriendsBinding
import www.dianbaozu.com.ui.chat.friend.adapter.FriendAdapter
import www.wen.com.common.lib.base.fragment.BaseVMFragment
import www.wen.com.common.lib.data.entities.FriendEntity
import www.wen.com.common.lib.data.entities.TopEntity
import www.wen.com.common.lib.data.entities.UnreadEntity
import www.wen.com.common.lib.extension.nav
import www.wen.com.common.lib.extension.observeObject
import www.wen.com.common.lib.extension.postObject
import www.wen.com.common.lib.helper.loading.KeyHelper

/**
 * @author WEN
 * @Description: 好友
 * @date 2020//25 17:48
 */
class FriendFragment : BaseVMFragment<FragmentFriendsBinding>() {
    private val friendEntityList = mutableListOf<FriendEntity>()
    private val friendAdapter by lazy { FriendAdapter(dataBinding.rvFriend, friendEntityList) }

    override fun getLayoutId(): Int = R.layout.fragment_friends

    override fun lazyInitView() {
        registerObservers(true)
        initData()

        dataBinding.apply {
            rvFriend.adapter = friendAdapter
            rvFriend.layoutManager = LinearLayoutManager(context)
            rvFriend.addOnItemTouchListener(touchListener)
        }

    }

    /**
     * 注册观察者
     */
    private fun registerObservers(register: Boolean) {
        NIMHelper.observeFriendChangedNotify(register) {
            notifyDataSetChanged()
        }
        NIMHelper.observeOnlineStateChange(register) {
            notifyDataSetChanged()
        }
        observeObject(KeyHelper.UNREAD_KEY,this,UnreadEntity::class.java){
            if (this.type == UnreadEntity.TYPE_ADD_FRIEND) {
                friendEntityList[0].topEntity?.unreadCount = this.unreadCount
                friendAdapter.notifyItemChanged(0)
            }
        }
    }

    private fun notifyDataSetChanged() {
        initData()
        friendAdapter.notifyDataSetChanged()
    }

    private val touchListener: SimpleClickListener<FriendAdapter> =
        object : SimpleClickListener<FriendAdapter>() {
            override fun onItemClick(adapter: FriendAdapter, view: View, position: Int) {
                adapter.getItem(position).friend?.let {
                    SessionHelper.startP2PSession(context, it.account)
                    return
                }
                adapter.getItem(position).topEntity?.onClick?.invoke()
            }

            override fun onItemLongClick(adapter: FriendAdapter, view: View, position: Int) {}
            override fun onItemChildClick(adapter: FriendAdapter, view: View, position: Int) {}
            override fun onItemChildLongClick(adapter: FriendAdapter, view: View, position: Int) {}
        }

    private fun initData() {
        friendEntityList.clear()
        friendEntityList.add(
            FriendEntity(
                topEntity = TopEntity(
                    R.drawable.ic_circle_add_friends,
                    "好友请求",
                    "查看新的好友添加请求",
                    NIMHelper.addFriendUnreadCount
                ) {
                    nav(R.id.action_mainFragment_to_friendRequestFragment)
                }
            )
        )
        friendEntityList.add(
            FriendEntity(
                topEntity = TopEntity(
                    R.drawable.ic_circle_shield,
                    "已屏蔽",
                    "查看你已屏蔽的用户",
                ) {
                    nav(R.id.action_mainFragment_to_refuseListFragment)
                }
            )
        )
        initFriends()
    }

    private fun initFriends() {
        var onlineFriendsCount = 0
        var offlineFriendsCount = 0
        NIMHelper.getAllFriend()?.let { allFriend ->
            allFriend.sortedBy {
                NIMHelper.accountIsOnline(it.account)
            }
            allFriend.forEach {
                val accountIsOnline = NIMHelper.accountIsOnline(it.account)
                if (accountIsOnline) {
                    onlineFriendsCount++
                } else {
                    offlineFriendsCount++
                }
            }

            allFriend.forEach {
                friendEntityList.add(
                    FriendEntity(
                        friend = it,
                        onlineFriendsCount = onlineFriendsCount,
                        offlineFriendsCount = offlineFriendsCount
                    )
                )
            }
        }
    }

    override fun onStop() {
        super.onStop()
        registerObservers(false)
    }

}



